<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_Zend
 * @subpackage Controller
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * Class that holds informations about db selection.
 * It holds names of columns and joined tables, not about the main table.
 *
 * @package SmartL_Zend
 * @subpackage Controller
 */
class SmartL_Zend_Controller_Action_Config_ActionInfo_SelectionInfo {
	private $_columns = array ();
	private $_joinTables = array ();
	
	/**
	 * Sets columns to be selected in db "select" query
	 *
	 * @param array $columns Columns to be selected
	 */
	public function setColumns(array $columns) {
		$this->_columns = $columns;
	}
	/**
	 * Returns selected columns list prepared for query. Columns that are not
	 * prefixed will gain specified prefix (usually used for selected main
	 * table columns).
	 *
	 * @param array $requiredColumns These columns will be included even if they are not in this selection.
	 * @param string $prefix Prefix for main table columns (default is "t")
	 * @return string Selected columns list prepared for query
	 */
	public function getColumnsForQuery(array $requiredColumns = array(), $prefix = "t") {
		$first = true;
		$result = '';
		foreach ( $this->_columns as $column ) {
			if (! $first)
				$result .= ',';
			if (strstr ( $column, "." ) === false) {
				$columnForQuery = $prefix . '.' . $column;
			} else {
				$columnForQuery = $column;
			}
			if (isset ( $requiredColumns [$columnForQuery] ))
				unset ( $requiredColumns [$columnForQuery] );
			$result .= $columnForQuery;
			$first = false;
		}
		foreach ( $requiredColumns as $columnForQuery ) {
			if (! $first)
				$result .= ",";
			$result .= $columnForQuery;
			$first = false;
		}
		
		return $result;
	}
	
	/**
	 * Returns Dql of this selection.
	 *
	 * @param string $tableName	Name of main table record.
	 * @param array $requiredColumns These columns will be included even if they are not in this selection.
	 * @param string $tablePrefix Prefix of main table.
	 * @return string
	 */
	public function getDql($tableName, array $requiredColumns = array(), $tablePrefix = "t") {
		return 'SELECT ' . $this->getColumnsForQuery ( $requiredColumns, $tablePrefix ) . " FROM {$tableName} {$tablePrefix}" . $this->getJoinTablesForQuery ( $tablePrefix );
	}
	/**
	 * Sets tables to be joined in db "select" query.
	 *
	 * @param array $tables[string joinType]=>$tableNames[numeric] Joined tables names
	 */
	public function setJoinTables(array $tables) {
		$this->_joinTables = $tables;
	}
	/**
	 * Returns joined tables list prepared for query.
	 *
	 * @return string Joindex tables list prepared for query
	 */
	public function getJoinTablesForQuery($mainTablePrefix) {
		$joinTables = array ();
		foreach ( $this->_joinTables as $type => $typeTables ) {
			foreach ( $typeTables as $table ) {
				if (strpos ( $table, '.' ) === false)
					$table = $mainTablePrefix . '.' . $table;
				$joinTables [$type] [] = $table;
			}
		}
		
		$result = '';
		foreach ( $joinTables as $type => $tables ) {
			$result .= ' ' . $type . ' ';
			$result .= implode ( ",", $tables );
		}
		return $result;
	}
}